/**
 * 
 */
package org.gov.vis.master.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.gui.util.ErrorsDisplayJPanel;
import org.gov.vis.master.controller.DepartmentCtlr;
import org.gov.vis.master.controller.DepartmentCtlrImpl;
import org.gov.vis.master.dto.DeptDto;
import org.gov.vis.master.gui.DepartmentDialog;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;

/**
 * @author user
 *
 */
public class DepartmentListener implements ActionListener,BaseListener{



	private DepartmentDialog departmentDailog;
	private DepartmentCtlr departmentCtlr = new DepartmentCtlrImpl();

	private ErrorsDisplayJPanel errorMsgPanel;
	final private ResourceBundle rb = Util.getResources();
	private DeptDto departmentDto;

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */

	public DepartmentListener(DepartmentDialog departmentDialog){

		this.departmentDailog = departmentDialog;
	}
	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		try{
			errorMsgPanel = new ErrorsDisplayJPanel(departmentDailog.getjPanel1());
			errorMsgPanel.clearMsgPanel();
			if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_save"))
					|| actionEvent.getActionCommand().equals(rb.getString("dialog_button_update"))){
				//Method called to either Add or Edit the Department details
				addOrEditDepartment();
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_view"))){			
				//Method called to view the details of Department Object based on the passed otherId
				viewDepartment(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_delete"))){
				//Method called to delete the Department Object based on the passed otherId.
				deleteDepartment(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_reset"))){
				//Method called to clear the screen data
				clear();
			}
		}catch(Exception e){
			e.printStackTrace();
		}

	}//end actionPerformed


	/**
	 * Method called to either Add or Edit the Department details
	 */
	private void addOrEditDepartment(){
		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

		//Method to set the Screen's data to  the DTO Object
		setGuiDetailsToDto(departmentDailog);

		try{
			//Method called to Add or Edit the Department
			departmentCtlr.addOrEditDepartment(departmentDto);
			/*List<String> successMsgLst = new ArrayList<String>();
			 successMsgLst.add(rb.getString(GuiConstants.MASTER_ADD_DEPARTMENT_SUCCESS));
			 errorMsgPanel.setSuccessMessages(successMsgLst);
			 
			 otherCtlr.addOrEditOther(otherDto);
			*/	JOptionPane.showMessageDialog(departmentDailog, rb.getString(GuiConstants.MASTER_ADD_DEPARTMENT_SUCCESS));
			//departmentDailog.dispose();	
			//Method called to clear the screen data
			clear();
		}catch(Exception e){
			List<String> errorMsgLst = new ArrayList<String>();
			if(e instanceof InvalidInputException){
				final HashMap<String,String> _exceptions = DepartmentCtlr.errorMsgs;
				if (_exceptions != null && !_exceptions.isEmpty()) {
					String errMsg = null;

					if(_exceptions.containsKey(rb.getString("dialog_label_dept_code").toUpperCase())){
						departmentDailog.getLblCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_dept_code").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_dept_institution").toUpperCase())){
						departmentDailog.getLblNameOfInst().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_dept_institution").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					errorMsgPanel.setErrorMessages(errorMsgLst);
				} else {
					//log.debug(e.getMessage(), e);
				}	
			}else{
				errorMsgLst.add(e.getMessage());
				errorMsgPanel.setErrorMessages(errorMsgLst);

			}
		}


	}//end addOrEditDepartment

	/**
	 * Method called to view the Department details.
	 */
	private void viewDepartment(final long _departmentId) {

		try{
			final DeptDto _departmentDto = departmentCtlr.getDepartment(_departmentId);
			setDtoDetailsToGui(_departmentDto);
			departmentDailog.validate();
			departmentDailog.repaint();
		}catch(Exception e){
			e.printStackTrace();
		}

	}//end viewDepartment

	/**
	 * Method called to delete the Department Object based on the passed otherId
	 * @param _otherId
	 */
	private void deleteDepartment(final long  _departmentId){
		try{
			departmentCtlr.deleteDepartment(_departmentId);
		}catch(Exception e){
			e.getMessage();
		}
	}//end deleteDepartment


	/**
	 * Method called to Clear all the form fields of the screen
	 */
	@Override
	public void clear() {
		// TODO Auto-generated method stub

		//set the Field's data to empty values
		departmentDailog.getTxtFldCode().setText("");
		departmentDailog.getTxtFldNameOfInst().setText("");
		departmentDailog.getTxtFldDistrict().setText("");
		departmentDailog.getTxtFldVillage().setText("");
		departmentDailog.getTxtFldManadal().setText("");

		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

		//ResultPanel
		//departmentDailog.getLblResultOrg().setText("");
		//departmentDailog.getLblrRsultCode().setText("");

	}//end clear

	/**
	 * Method called to read the data from GUI and set them to DTO Object 
	 * @param _guiObject
	 */
	@Override
	public void setDtoDetailsToGui(Object object) {
		// TODO Auto-generated method stub
		DeptDto _departmentDto = (DeptDto)object;
		departmentDailog.getTxtFldCode().setText(_departmentDto.getCode());
		departmentDailog.getTxtFldDistrict().setText(_departmentDto.getDistrict());
		departmentDailog.getTxtFldVillage().setText(_departmentDto.getVillage());
		departmentDailog.getTxtFldManadal().setText(_departmentDto.getMandal());
		departmentDailog.getTxtFldNameOfInst().setText(_departmentDto.getInstitution());
		//	departmentDailog.getCombxEmployeeType().setSelectedIndex(0);

	}//end setDtoDetailsToGui

	/**
	 * Method called to read the data from DTO and set them to GUI.
	 * @param _guiObject
	 */
	@Override
	public void setGuiDetailsToDto(Object object) {
		// TODO Auto-generated method stub
		DepartmentDialog _departmentDialog =(DepartmentDialog)object;
		departmentDto = new DeptDto();
		departmentDto.setCode(_departmentDialog.getTxtFldCode().getText());
		departmentDto.setDistrict(_departmentDialog.getTxtFldDistrict().getText());
		departmentDto.setInstitution(_departmentDialog.getTxtFldNameOfInst().getText());
		departmentDto.setMandal(_departmentDialog.getTxtFldManadal().getText());
		departmentDto.setVillage(_departmentDialog.getTxtFldVillage().getText());

	}//end setGuiDetailsToDto

	public void setDepartmentDailog(DepartmentDialog departmentDailog) {
		this.departmentDailog = departmentDailog;
	}

	public DepartmentDialog getDepartmentDailog() {
		return departmentDailog;
	}

	public void setDepartmentCtlr(DepartmentCtlr departmentCtlr) {
		this.departmentCtlr = departmentCtlr;
	}

	public DepartmentCtlr getDepartmentCtlr() {
		return departmentCtlr;
	}
	@Override
	public void clearGUILabelColors(Color _lblColor) {
		//set the Field's Labels to default Black color
		departmentDailog.getLblCode().setForeground(_lblColor);
		departmentDailog.getLblNameOfInst().setForeground(_lblColor);
		departmentDailog.getLblDistrict().setForeground(_lblColor);
		departmentDailog.getLblMandal().setForeground(_lblColor);
		departmentDailog.getLblVillage().setForeground(_lblColor);

	}




}//end class
